//
//  JBWBaseResponse.m
//  DDFF
//
//  Created by 崔志伟 on 2018/7/26.
//  Copyright © 2018年 崔志伟. All rights reserved.
//

#import "JBWBaseResponse.h"

@interface JBWBaseResponse ()
/*
 开始请求回调
 */
@property (nonatomic,copy) JBWNetHTTPStartRequest startRequest;
/*
 完成请求回调
 */
@property (nonatomic,copy) JBWNetHTTPFinishRequest finishRequest;

/*
 请求错误回调
 */
@property (nonatomic,copy) JBWNetHTTPErrorRequest errorRequest;

@end

@implementation JBWBaseResponse


- (void)createPostRequest
{
    //构建对象
    if (![self getUrl]) {
        return;
    }
    JBWNetHTTPRequest* request = [[JBWNetHTTPRequest alloc] initWithUrlString:[self getUrl]];
    //网络请求方式  可以不写   LHHTTPRequest默认情况下  如果没有请求体则是GET，存在请求体则是POST
    request.requestMethod = @"POST";
    //请求超时时间
    request.requestTimeOut = 30.0f;
    //传入字典
    if ([self requestBody]) {
        request.requestBody = [self requestBody];
    }
    //请求头
    if ([self requestHeaderField]) {
        request.requestHeaderField = [self requestHeaderField];
    }
    //请求开始
    [request requestStart:self.startRequest];
    //开始接收数据   如果数据量大  则会多次回调此方法
    [request requestReceiveData:^(NSData *data) {
    }];
    //回调进度 currentBytes:本次接收的数据大小 alreadyReceiveBytes:已经接收的数据大小 totalBytes:数据总大小
    [request requestUpdateProgress:^(JBWNetProgressLong currentBytes, JBWNetProgressLong alreadyReceiveBytes, JBWNetProgressLong totalBytes) {
    }];
    //网络请求结束后回调的block  reslutData为总数据
    [request requestFinish:self.finishRequest];

    //网络请求错误回调
    [request requestError:self.errorRequest];
    //取消下载回调
    [request requestCancel:^{
    }];
    //异步请求
    [request startAsynchronous];
    
}


- (void)createGetRequest
{
    //构建对象
    if (![self getUrl]) {
        return;
    }
    JBWNetHTTPRequest* request = [[JBWNetHTTPRequest alloc] initWithUrlString:[self getUrl]];
    
    //网络请求方式  可以不写   LHHTTPRequest默认情况下  如果没有请求体则是GET，存在请求体则是POST
    request.requestMethod = @"GET";
    //请求超时时间
    request.requestTimeOut = 30.0f;
    //请求头
    if ([self requestHeaderField]) {
        request.requestHeaderField = [self requestHeaderField];
    }
    //请求开始
    [request requestStart:self.startRequest];
    //网络请求结束后回调的block  reslutData为总数据
    [request requestFinish:self.finishRequest];
    //网络请求错误回调
    [request requestError:self.errorRequest];
    //取消下载回调
    [request requestCancel:^{
    }];
    //异步请求
    [request startAsynchronous];
    
}

- (void)postRequest:(JBWNetHTTPStartRequest )startRequest finishRequest:(JBWNetHTTPFinishRequest)finishRequest errorRequest:(JBWNetHTTPErrorRequest)errorRequest{
    self.startRequest = startRequest;
    self.finishRequest = finishRequest;
    self.errorRequest = errorRequest;
    [self createPostRequest];
    
}

- (void)getRequest:(JBWNetHTTPStartRequest )startRequest finishRequest:(JBWNetHTTPFinishRequest)finishRequest errorRequest:(JBWNetHTTPErrorRequest)errorRequest{
    self.startRequest = startRequest;
    self.finishRequest = finishRequest;
    self.errorRequest = errorRequest;
    [self createPostRequest];
}

- (NSString *)getUrl{
   return nil;
    
}

- (NSMutableDictionary *)requestBody{
    return nil;
    
}

- (NSMutableDictionary *)requestHeaderField{
    return nil;;
    
}

- (NSDictionary *)jSONObjectWithData:(NSData *)resultData{
  
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingMutableContainers error:nil];
    return dic;
}
- (NSString *)getParameters:(NSMutableDictionary *)parameters{
    NSMutableString *mutableUrl = [[NSMutableString alloc] initWithString:[self getUrl]];
    if ([parameters allKeys]) {
        [mutableUrl appendString:@"?"];
        for (id key in parameters) {
            NSString *value = [[parameters objectForKey:key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [mutableUrl appendString:[NSString stringWithFormat:@"%@=%@&", key, value]];
        }
    }
    NSString *urlEnCode = [[mutableUrl substringToIndex:mutableUrl.length - 1] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    return urlEnCode;
}


@end
